package com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.TongJiInfo;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.dao.CaseSelectionManagementDao;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.CaseSelectionManagementInfo;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.SearchCaseSelectionManagementInfo;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.ICaseSelectionManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CaseSelectionManagementServiceImpl implements ICaseSelectionManagementService {
    @Autowired
    private CaseSelectionManagementDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    public List<CaseSelectionManagementInfo> selectPageListByQueryGetMap(Page<CaseSelectionManagementInfo> page, SearchCaseSelectionManagementInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
                params.put("startTime", " and t.selection_time >= '" + searchInfo.getStartTime() + "'");
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
                params.put("endTime", " and t.selection_time <= '" + searchInfo.getEndTime() + "'");
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPages(page, params);
    }

    public int getQueryPagesCount(Page<CaseSelectionManagementInfo> page, SearchCaseSelectionManagementInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
                params.put("startTime", " and t.selection_time >= '" + searchInfo.getStartTime() + "'");
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
                params.put("endTime", " and t.selection_time <= '" + searchInfo.getEndTime() + "'");
        }
        return dao.getQueryPagesCount(page, params);
    }

    public List<TongJiInfo> caseSelectionManagementTotal(SearchCaseSelectionManagementInfo searchInfo) {
        String contitionInfo = " ";
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            contitionInfo += " and t.selection_time >= '" + searchInfo.getStartTime() + "' ";
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            contitionInfo += " and t.selection_time >= '" + searchInfo.getStartTime() + "' ";
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            contitionInfo += " and (t.customer_name like CONCAT('%" + searchInfo.getReach() + "%') or t.szm like CONCAT('%" + searchInfo.getReach() + "%')) ";
        }
        String sql = "select count(1) as countNum from (select " +
                "  t.id as id " +
                "  from case_selection_information t left join case_selection_info t1 on t.id=t1.information_id " +
                "  where 1=1 and save_status =1 and t1.selection_status =1 "+contitionInfo+" group by t.id) t ";
        return sqlMapper.selectList(sql, TongJiInfo.class);
    }

    @Override
    public List<String> getAllCasesIds() {
        return dao.getAllCasesIds();
    }

    @Override
    public List<String> getAllExpertIds() {
        return dao.getAllExpertIds();
    }

    @Override
    public void insertExpertAndCase(String expertId,String []ids) {
        dao.insertExpertAndCase(expertId,ids);
    }

    @Override
    public void updateCasesForDistribution(String[] ids) {
        dao.updateCasesForDistribution(ids);
    }

    @Override
    public String isVoteToday(String openid, String caseid) {
        return dao.isVoteToday(openid,caseid);
    }

    @Override
    public void insertCaseWechatVote(String openid, String caseid) {
        dao.insertCaseWechatVote(openid,caseid);
    }

    @Override
    public void updateCaseVote(String caseid) {
        dao.updateCaseVote(caseid);
    }
}